package com.gjzt.business.customer.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.eastdigit.system.model.User;
import com.eastdigit.system.service.UserService;
import com.gjzt.business.customer.service.CustomerService;

@Controller
@RequestMapping(value = "/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    
    @Autowired
    UserService userService;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryAdCustomer(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(customerService.findAdCustomer(reqBean));
    }
    
    @RequestMapping(value = "/{customerId}/info")
    @ResponseBody
    public JSONObject getInfo(@PathVariable String customerId) {
        return Response.response(customerService.getCusomerInfo(customerId).toJsonObject());
    }
    
    @RequestMapping(value = "/{customerId}/user")
    @ResponseBody
    public JSONObject getUserInfo(@PathVariable String customerId) {
        return Response.response(userService.queryBySource(User.TYPE_CUSTOMER, customerId));
    }
    
    @RequestMapping(value = "/{customerId}/userSetting")
    @ResponseBody
    public JSONObject settingUser(@PathVariable String customerId,final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	customerService.settingCustomerUser(customerId, reqBean);
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/resetPassword")
    @ResponseBody
    public JSONObject resetPassword(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        userService.resetPassword(reqBean.getInfo("userId"));
        return Response.responseSuccess();
    }
    
    /**
     * 查询客户资源排期
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/findCustomerResSchedule")
    @ResponseBody
    public JSONObject findCustomerResSchedule(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
    	
        return Response.response(customerService.findCustomerResSchedule(reqBean));
    }
    
    @RequestMapping(value = "/queryContract")
    @ResponseBody
    public JSONObject queryContract(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(customerService.queryContract(reqBean));
    }

    @RequestMapping(value = "/queryCustomer")
    @ResponseBody
    public JSONObject queryCustomerTree(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(customerService.queryCustomer(reqBean));
    }
    
    
    @RequestMapping(value = "/getContracts4Customer")
    @ResponseBody
    public JSONObject getContracts4Customer(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(customerService.getContracts4Customer(reqBean));
    }

    /**
     * 添加
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject addAdCustomer(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        customerService.addAdCustomer(reqBean);
        return Response.responseSuccess();
    }

    /**
     * 修改
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject updateAdCustomer(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        customerService.updateAdCustomer(reqBean);
        return Response.responseSuccess();
    }

    /**
     * 失效
     *
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject removeAdCustomer(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        customerService.disableCustomer(reqBean);
        return Response.responseSuccess();
    }

    //TODO 其他功能
}